<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class LevelsApiController extends AbstractRestfulController
{
    public function getList()
    {			
		header('Access-Control-Allow-Origin: *');
		$questionsTable=$this->getServiceLocator()->get('Models\Model\QuestionsFactory');
		$questionsList = $questionsTable->getQuestionslist()->toarray();
		if(count($questionsList)!=0){
			return new JsonModel(array(
				'questionsList' 	=> json_encode($questionsList),
				'value'				=>	1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'no data',
				'value'	 =>	1,
			));
		}	
    }
    public function get($course_id)
    {		
		header('Access-Control-Allow-Origin: *');
		$questionsTable=$this->getServiceLocator()->get('Models\Model\QuestionsFactory');
		$levelCompletedTable=$this->getServiceLocator()->get('Models\Model\LevelCompletedFactory');
		$getLevel="";
		$explodeId=explode('-',$course_id);
			$courseId=$explodeId[0];
			$subject_id=$explodeId[1];
			$user_id=$explodeId[2];
		$checkUser = $levelCompletedTable->checkUser($user_id)->toArray();
		if(count($checkUser)!=0){
			$getlev = $levelCompletedTable->getUserLevel($user_id,$courseId,$subject_id)->toArray();
			$getLevel=$getlev[0]['level']; 
		}else{
			$getLevel='0';
		}
		$levelList = $questionsTable->getlevelList($courseId,$subject_id)->toarray();
		if(count($levelList)!=0){
			return new JsonModel(array(
				'levelList' 	=> json_encode($levelList),
				'getLevel' 		=> $getLevel,
				'value'			=>	1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'no data',
				'value'	=>	0,
			));
		}	
    }
    public function create($data)
    {
		header('Access-Control-Allow-Origin: *');	
		$questionTable=$this->getServiceLocator()->get('Models\Model\QuestionsFactory');
		$answerTable=$this->getServiceLocator()->get('Models\Model\AnswersFactory');
		$saveQuestions = $questionTable->saveQuestions($data);
		$saveAnswer=$answerTable->saveAnswer($saveQuestions,$data['correct_answer']);
		if($saveQuestions!=0){
			return new JsonModel(array(
				'output' 	=> 	'success',
				'QuestionId'	=>	$saveQuestions,
				'value' 	=> 	1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'fail',
				'value' => 0,
			));
		}
    }
    public function update($uid, $data)
    {
		header('Access-Control-Allow-Origin: *');	
		$userTable=$this->getServiceLocator()->get('Models\Model\UserFactory');
		$userDetailsTable=$this->getServiceLocator()->get('Models\Model\UserDetailsFactory');
		$userRegistration = $userTable->updateUserData($data,$uid);
		$userRegistrationDetails = $userDetailsTable->updateUserDetails($data,$uid);
		if($userRegistrationDetails){
			return new JsonModel(array(
				'output' => 'success',
				'value' => 1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'fail',
				'value' => 0,
			));
		}
    }
    public function delete($id)
    {
       
    }
}